package com.taofengsdk.video;

import android.view.ViewGroup;

import com.taofengsdk.adutil.Utils;
import com.taofengsdk.constant.SDKConstant;
import com.taofengsdk.model.AdValue;
import com.taofengsdk.video.core.AdContextInterface;
import com.taofengsdk.video.core.VideoAdSlot;
import com.taofengsdk.video.json.ResponseEntity;
import com.taofengsdk.video.report.ReportManager;
import com.taofengsdk.widegt.CustomVideoView;

/**
 * Created by tao on 17-3-16.
 * 管理slot, 与外界进行通信
 */

public class VideoAdManager implements VideoAdSlot.AdSDKSlotListener {

    private ViewGroup mParentView;
    private AdValue mInstance = null;
    private VideoAdSlot mAdSlot;

    private CustomVideoView.ADFrameImageLoadListener mFrameLoadListener;
    private AdContextInterface mListener;

    public VideoAdManager() {

    }

    public VideoAdManager(ViewGroup parentView, String instance,
                          CustomVideoView.ADFrameImageLoadListener frameImageLoadListener) {
        this.mParentView = parentView;
        this.mFrameLoadListener = frameImageLoadListener;
        mInstance = (AdValue) ResponseEntity.parseJson(instance, AdValue.class);
        load();
    }

    /**
     * 创建业务逻辑层类,不调用则不会创建CustomVideoview
     */
    private void load() {
        if (mInstance != null && mInstance.resource != null) {
            mAdSlot = new VideoAdSlot(mInstance, this, mFrameLoadListener);
            //发送解析成功事件
            sendAnalizeReport(SDKConstant.Params.ad_analize, SDKConstant.AD_DATA_SUCCESS);
        } else {
            mAdSlot = new VideoAdSlot(null, this, mFrameLoadListener); //创建空的slot,不响应任何事件
            if (mListener != null) {
                mListener.onAdFailed();
            }
            sendAnalizeReport(SDKConstant.Params.ad_analize, SDKConstant.AD_DATA_FAILED);
        }

    }

    /**
     * release the ad
     */
    public void destroy() {
        mAdSlot.destory();
    }

    public void setAdResultListener(AdContextInterface listener) {
        this.mListener = listener;
    }


    /**
     * 根据滑动距离来判断是否可以自动播放, 出现超过50%自动播放，离开超过50%,自动暂停
     */
    public void updateVideoInScrollView() {
        if (mAdSlot != null) {
            mAdSlot.updateVideoInScrollView();
        }
    }

    @Override
    public ViewGroup getAdParent() {
        return mParentView;
    }

    @Override
    public void onAdVideoLoadSuccess() {
        if (mListener != null) {
            mListener.onAdSuccess();
        }
        sendAnalizeReport(SDKConstant.Params.ad_load, SDKConstant.AD_PLAY_SUCCESS);
    }

    @Override
    public void onAdVideoLoadFailed() {
        if (mListener != null) {
            mListener.onAdFailed();
        }
        sendAnalizeReport(SDKConstant.Params.ad_load, SDKConstant.AD_PLAY_FAILED);
    }

    @Override
    public void onAdVideoLoadComplete() {

    }

    @Override
    public void onClickVideo(String url) {

    }

    @Override
    public void changeFagmentOrActivity() {
        if (mAdSlot != null) {
            mAdSlot.changeFagmentOrActivity();
        }
    }

    /**
     * 发送广告数据解析成功监测
     */
    private void sendAnalizeReport(SDKConstant.Params step, String result) {
        try {
            ReportManager.sendAdMonitor(Utils.isPad(mParentView.getContext().
                            getApplicationContext()), mInstance == null ? "" : mInstance.resourceID,
                    (mInstance == null ? null : mInstance.adid), Utils.getAppVersion(mParentView.getContext()
                            .getApplicationContext()), step, result);
        } catch (Exception e) {

        }
    }
}
